const express = require("express");

const pool = require("../pool.js");

const r = express.Router();

// 添加到购物车的接口
r.post("/addCart", (req, res) => {
  // 获取蛋糕的id，判断该蛋糕在购物车是否存在
  let cid = req.body.cid;
  // 获取对应的用户id，判断该用户的购物车
  let uid = req.body.uid;
  let title = req.body.title;
  let pic = req.body.pic;
  // 蛋糕的配方
  let size = req.body.size;
  let sandwich = req.body.sandwich;
  let composition = req.body.composition;
  let price = req.body.price;
  let cake_count = req.body.count;

  let selCakeSql =
    "SELECT COUNT(shop_id) as count FROM shoppingcart,cake WHERE shoppingcart.shop_cake_id=cake.c_id AND shop_cake_id=? AND user_id=? AND size=? AND sandwich=? AND composition=?;";
  pool.query(
    selCakeSql,
    [cid, uid, size, sandwich, composition],
    (err, selCakeResult) => {
      if (err) throw err;
      let testCount = selCakeResult[0].count;
      // 为0则代表该蛋糕没有加入购物车
      if (testCount == 0) {
        let insertSql =
          "insert into shoppingcart(user_id,shop_cake_id,shop_count,cake_title,cake_pic,size,sandwich,composition,price) VALUES(?,?,?,?,?,?,?,?,?);";
        pool.query(
          insertSql,
          [
            uid,
            cid,
            cake_count,
            title,
            pic,
            size,
            sandwich,
            composition,
            price,
          ],
          (err, insertResult) => {
            if (err) throw err;
            res.send({ code: 200, msg: "insertCart ok" });
          }
        );
      } else {
        let updateSql =
          "UPDATE shoppingcart SET shop_count=shop_count+1 WHERE shop_cake_id=? AND user_id=? AND size=? AND sandwich=? AND composition=?;";
        pool.query(
          updateSql,
          [cid, uid, size, sandwich, composition],
          (err, updateResult) => {
            if (err) throw err;
            res.send({ code: 200, msg: "updateCart ok" });
          }
        );
      }
    }
  );
});

r.get("/selCart", (req, res) => {

  let uid = req.query.uid;
  let selSql = "SELECT * FROM `shoppingcart` WHERE user_id=?";
  pool.query(selSql, [uid], (err, selCartResult) => {
    if (err) throw err;
    if (selCartResult.length == 0) {
      res.send({ code: 201, msg: "no shop" });
    } else {
      res.send({ code: 200, msg: "ok", selCartResult: selCartResult });
    }
  });
});

// 修改蛋糕数量
r.put("/changeCount", (req, res) => {
  let changeId = req.body.shopId;
  let symbol = req.body.symbol;
  console.log(symbol);
  console.log(changeId);
  if (symbol == "-") {
    let updateSql =
      "update shoppingcart set shop_count=shop_count-1 WHERE shop_id=?;";
    pool.query(updateSql, [changeId], (err, updateResult) => {
      if (err) throw err;
      if (updateResult.affectedRows == 1)
        res.send({ code: 200, msg: "update ok" });
      else res.send({ code: 201, msg: "no found shopId" });
    });
  } else {
    let updateSql =
      "update shoppingcart set shop_count=shop_count+1 WHERE shop_id=?;";
    pool.query(updateSql, [changeId], (err, updateResult) => {
      if (err) throw err;
      if (updateResult.affectedRows == 1)
        res.send({ code: 200, msg: "update ok" });
      else res.send({ code: 201, msg: "no found shopId" });
    });
  }
});

// 删除指定购物车蛋糕
r.delete("/delCarCake/:delShopId", (req, res) => {
  console.log(req);
  let del_shopid = req.params.delShopId;
  console.log(del_shopid);
  let delSql = "DELETE FROM shoppingcart WHERE shop_id=?;";
  pool.query(delSql, [del_shopid], (err, delResult) => {
    if (err) throw err;
    if (delResult.affectedRows == 1) {
      res.send({ code: 200, msg: "delete ok" });
    } else {
      res.send({ code: 201, msg: "no found shopId" });
    }
  });
});

module.exports = r;
